package routes

import (
	"admin-glm-go/internal/config"
	"admin-glm-go/internal/controllers"
	"admin-glm-go/internal/middleware"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

// InitRoutes 初始化路由
func InitRoutes(r *gin.Engine, db *gorm.DB, cfg *config.Config) {
	// 使用CORS中间件
	r.Use(middleware.CORSMiddleware())
	
	// 创建控制器实例
	authController := controllers.NewAuthController(db)
	adminController := controllers.NewAdminController(db)
	roleController := controllers.NewRoleController(db)
	permissionController := controllers.NewPermissionController(db)
	menuController := controllers.NewMenuController(db)
	articleController := controllers.NewArticleController(db)

	// API分组
	api := r.Group("/api")
	
	// 不需要认证的接口
	noAuth := api.Group("")
	{
		// 认证相关
		noAuth.POST("/auth/login", authController.Login)
	}

	// 需要认证的接口
	authRequired := api.Group("")
	// 从配置中获取JWT密钥
	authRequired.Use(middleware.JWTAuth(cfg.JWTSecret))
	{
		// 认证相关
		authRequired.POST("/auth/logout", authController.Logout)
		authRequired.GET("/auth/current", authController.Current)

		// 管理员管理
		adminGroup := authRequired.Group("/admin")
		{
			adminGroup.GET("/list", adminController.GetAdminList)
			adminGroup.POST("/add", adminController.AddAdmin)
			adminGroup.PUT("/update", adminController.UpdateAdmin)
			adminGroup.DELETE("/delete/:id", adminController.DeleteAdmin)
			adminGroup.PUT("/reset-password", adminController.ResetPassword)
		}

		// 角色管理
		roleGroup := authRequired.Group("/role")
		{
			roleGroup.GET("/list", roleController.GetRoleList)
			roleGroup.POST("/add", roleController.AddRole)
			roleGroup.PUT("/update", roleController.UpdateRole)
			roleGroup.DELETE("/delete/:id", roleController.DeleteRole)
			roleGroup.GET("/all", roleController.GetAllRoles)
		}

		// 权限管理
		permissionGroup := authRequired.Group("/permission")
		{
			permissionGroup.GET("/list", permissionController.GetPermissionList)
			permissionGroup.GET("/tree", permissionController.GetPermissionTree)
			permissionGroup.POST("/add", permissionController.AddPermission)
			permissionGroup.PUT("/update", permissionController.UpdatePermission)
			permissionGroup.DELETE("/delete/:id", permissionController.DeletePermission)
		}

		// 菜单管理
		menuGroup := authRequired.Group("/menu")
		{
			menuGroup.GET("/list", menuController.GetMenuList)
			menuGroup.GET("/tree", menuController.GetMenuTree)
			menuGroup.GET("/user-menu", menuController.GetUserMenu)
			menuGroup.POST("/add", menuController.AddMenu)
			menuGroup.PUT("/update", menuController.UpdateMenu)
			menuGroup.DELETE("/delete/:id", menuController.DeleteMenu)
		}

		// 文章管理
		articleGroup := authRequired.Group("/article")
		{
			articleGroup.GET("/list", articleController.GetArticleList)
			articleGroup.GET("/detail/:id", articleController.GetArticleDetail)
			articleGroup.POST("/add", articleController.AddArticle)
			articleGroup.PUT("/update", articleController.UpdateArticle)
			articleGroup.DELETE("/delete/:id", articleController.DeleteArticle)

			// 分类管理
			categoryGroup := articleGroup.Group("/category")
			{
				categoryGroup.GET("/list", articleController.GetCategoryList)
				categoryGroup.GET("/all", articleController.GetAllCategories)
				categoryGroup.POST("/add", articleController.AddCategory)
				categoryGroup.PUT("/update", articleController.UpdateCategory)
				categoryGroup.DELETE("/delete/:id", articleController.DeleteCategory)
			}
		}
	}
}